package dao.Impl;

import dao.BaseDao;
import dao.C_IdDao;
import model.VO.C_SetmealVO;
import model.VO.DishItemVO;
import model.VO.DishListVO;
import model.entity.Setmeal;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class C_IdDaoImpl extends BaseDao implements C_IdDao {

    @Override
    public List<DishItemVO> find(int id) {
        List<DishItemVO> dishItemVOList=new ArrayList<>();
        String sql="select setmeal_dish.name,setmeal_dish.copies,dish.description,dish.image from dish,setmeal_dish where setmeal_dish.setmeal_id=? and dish.id=setmeal_dish.dish_id;";
        try{
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            pstmt.executeQuery();
            rs= pstmt.getResultSet();
            while (rs.next()){
                DishItemVO d=new DishItemVO();
                d.setName(rs.getString("name"));
                d.setCopies(rs.getInt("copies"));
                d.setDescription(rs.getString("description"));
                d.setImage(rs.getString("image"));
                dishItemVOList.add(d);
            }
            System.out.println("按套餐id查询菜品成功!");
        }
        catch (SQLException e){
            System.out.println("按套餐id查询菜品失败!"+e);
        }
        return dishItemVOList;
    }

    @Override
    public List<C_SetmealVO> Find(int id) {
        List<C_SetmealVO> List=new ArrayList<>();
        String sql="select id,image,name,price,description from setmeal where category_id=?;";
        try{
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            pstmt.executeQuery();
            rs= pstmt.getResultSet();
            while (rs.next()){
                C_SetmealVO v=new C_SetmealVO();
                v.setId(rs.getInt("id"));
                v.setImage(rs.getString("image"));
                v.setName(rs.getString("name"));
                v.setPrice(rs.getFloat("price"));
                v.setDescription(rs.getString("description"));
                List.add(v);
            }
            System.out.println("按分类id查询套餐成功!");
        }
        catch (SQLException e){
            System.out.println("按分类id查询套餐失败!"+e);
        }
        return List;
    }

    @Override
    public List<DishListVO> FIND(int id) {
        List<DishListVO> dishListVOS=new ArrayList<>();
        String sql="select dish.category_id,dish.name,dish.description,dish.id,dish.image,category.name,dish.price,dish.status,dish.update_time from dish,category where dish.category_id=? and dish.category_id=category.id;";
        try{
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            pstmt.executeQuery();
            rs= pstmt.getResultSet();
            while (rs.next()){
                DishListVO v=new DishListVO();
                v.setCategoryId(rs.getInt("category_id"));
                v.setCategoryName(rs.getString("category.name"));
                v.setName(rs.getString("name"));
                v.setStatus(rs.getInt("status"));
                v.setUpdateTime(String.valueOf(rs.getTimestamp("update_time")));
                v.setDescription(rs.getString("description"));
                v.setImage(rs.getString("image"));
                v.setPrice(rs.getFloat("price"));
                v.setId(rs.getInt("id"));
                dishListVOS.add(v);
            }
            System.out.println("按分类id查询菜品成功!");
        }
        catch (SQLException e){
            System.out.println("按分类id查询菜品失败!"+e);
        }
        return dishListVOS;
    }

    @Override
    public Setmeal getById(int id) {
        Setmeal setmeal = new Setmeal();
        String sql="select * from `setmeal` where id=?;";
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            pstmt.executeQuery();
            rs=pstmt.getResultSet();
            while (rs.next()){
                setmeal.setId(rs.getInt("id"));
                setmeal.setName(rs.getString("name"));
                setmeal.setDescription(rs.getString("description"));
                setmeal.setPrice(rs.getFloat("price"));
                setmeal.setImage(rs.getString("image"));
                setmeal.setStatus(rs.getInt("status"));
                setmeal.setCategory_id(rs.getInt("category_id"));
                setmeal.setCreate_time(String.valueOf(rs.getTime("create_time")));
                setmeal.setUpdate_time(String.valueOf(rs.getTime("update_time")));
                setmeal.setCreate_user(rs.getInt("create_user"));
                setmeal.setUpdate_user(rs.getInt("update_user"));
            }
        } catch (SQLException e) {
            System.out.println("根据id查询菜品错误："+e);
        }
        return setmeal;
    }
}
